package org.shreker.shan.website.business.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import org.shreker.shan.core.domain.Log;
import org.shreker.shan.core.queryobject.WebLogQueryObject;
import org.shreker.shan.core.service.ILogService;
import org.shreker.shan.core.util.LoginContext;
import org.shreker.shan.rbac.annotation.PermissionRequired;
import org.shreker.shan.website.base.controller.WebSiteController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class WebSiteLogController extends WebSiteController {

	@Autowired
	private ILogService logService;

	@PermissionRequired("查看日志")
	//@RequestMapping(value="/loglist", method={RequestMethod.POST})
	@RequestMapping("/log")
	public String log(@ModelAttribute("queryObject") WebLogQueryObject queryObject, Model model) {
		// 在SpringMVC中, 方法的所有参数都会默认添加到Model中, 默认的名称是这个对象的类型的名字首字母小写
		// 如果需要修改默认的名称, 在参数前添加一个注解@ModelAttribute("<your-name>")
		queryObject.setUserId(LoginContext.getCurrentLogin().getId());
		List<Log> logs = logService.query(queryObject);
		model.addAttribute("logs", logs);
		return "log_list";
	}

	@InitBinder
	public void initBinder(WebDataBinder binder) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		sdf.setLenient(false);
		binder.registerCustomEditor(Date.class, new CustomDateEditor(sdf, true));
	}

}
